<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Course_banke extends MY_Controller {

	public $left_menu_min;

	function __construct(){
	parent::__construct();

		$parent_id = 3;
		//获取school二级菜单
		$this->load->model('School_manage_menu_model');
	    $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
	    // print_r($menu_list);exit;
	    $this->smarty->assign("menu_list", $menu_list);

		$this->left_menu_min = array("menu" => "课程管理", "curr" => '/course_banke', "list" => $menu_list);
	  	$this->smarty->assign("left_menu_min", $this->left_menu_min);
	  	$this->smarty->assign("menu", "course");

	}



	/**
	 * 班课列表
	 */
	public function index($status = 10, $name = 0, $cate_id = 0, $kemu = 0, $limit = 0)
	{
		$post_data = $this->input->post();

		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$name = !empty($post_data['name']) ? $post_data['name'] : 0;
			$kemu = !empty($post_data['kemu']) ? $post_data['kemu'] : 0;
			$cate_id = !empty($post_data['cate_id']) ? $post_data['cate_id'] : 0;
		}

		$this->load->model('Courseo2o_model');
		$conditions['name'] = $name;
		$conditions['status'] = $status;
		$conditions['school_id'] = $this->user_data['school_id'];
		$conditions['cate_id'] = $cate_id;
		$conditions['kemu'] = $kemu;
		$result = $this->Courseo2o_model->get_course_by_school($limit, $this->offset, $conditions);

		$this->load->model("Courses");
		$cate = $this->Courses->get_cate(2);
		$subject = $this->Courseo2o_model->get_offline_course($this->user_data['school_id'],$cate_id);

		$this->load->library('pagination');
		$page_config['base_url'] = '/course_banke/index/'.$status.'/'.$name.'/'.$cate_id.'/'.$kemu;
		$page_config['total_rows'] = $result['total_count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 7;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->smarty->assign("course", $result['result']);
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("school_domain", $this->user_data['school_domain']);
		$this->smarty->display("banke/course_banke_list.html");
	}

	/**
	 * 删除面授课
	 * @param  [type] $course_id [description]
	 * @return [type]            [description]
	 */
	public function del($course_id = 0)
	{
		if(empty($course_id)){
			show_message("/course_banke", "非法操作");
		}else{
			delete_data((array)$course_id, "course");
			show_message("/course_banke");
		}
	}

	/**
	*	发布班课
	*/
	public function publish()
	{
		$token = get_qiniu_token('VIMG');
		$this->smarty->assign("token", $token);
		$this->smarty->assign("subject", "");
		$this->load->model('Courseo2o_model');

		//post data
		$post_data = $this->input->post();
		$has_error = 0;

		if($post_data){

			if(empty($post_data['title'])){
				$this->smarty->assign("title_err", "班课标题不能为空");
				$has_error = 1;
			}else{
				$this->smarty->assign("title", $post_data['title']);
			}

			if(empty($post_data['course_time'])){
				$this->smarty->assign("course_time_err", "请填写总课时");
				$has_error = 1;
			}else{
				$this->smarty->assign("course_time", $post_data['course_time']);
			}
			// if(empty($post_data['price'])){
   //      $this->smarty->assign("price_err", "请填写价格");
   //      $has_error = 1;
   //    }else{
			// 	$this->smarty->assign("price", $post_data['price']);
			// }

			if(!empty($post_data['class_number'])){
				$this->smarty->assign("class_number", $post_data['class_number']);
			}
			if(!empty($post_data['suitable'])){
       			$this->smarty->assign("suitable", $post_data['suitable']);
			}

			if(!empty($post_data['learn_what'])){
           		$this->smarty->assign("learn_what", $post_data['learn_what']);
			}
			if(empty($post_data['subject'])){
				$this->smarty->assign("subject_err", "请选择科目");
				$has_error = 1;
			}else{
				$this->smarty->assign("subject", $post_data['subject']);
			}

			if(empty($post_data['detail'])){
				$this->smarty->assign("desc_err", "请填写课程详情");
				$has_error = 1;
			}else{
				$this->smarty->assign("detail", $post_data['detail']);
			}

			if(!empty($post_data['course_imgs'])){
				$imgs = array();
				$i = 0;
				foreach ($post_data['course_imgs'] as $k => $v) {
					if($i > 5){
						break;
					}
					$imgs[] = get_qiniu_url($v);
					$i++;
				}
				$post_data['course_imgs'] = $imgs;
			}

			//插入班课
			if($has_error == 0){
				// print_r($post_data);exit;
				$this->Courseo2o_model->publish_course_by_school($this->user_data['id'],$this->user_data['teacher_id'],$this->user_data['school_id'],$post_data);
				header("Location:/course_banke");exit;
			}

			// print_r($post_data['img_url'][0]);
			// print_r(json_encode($post_data['img_url']));exit;
		}

		//取线下课程分类
    	$cate_list = $this->Courseo2o_model->get_offline_cate($this->user_data['school_id']);
    	$this->smarty->assign("cate_list", $cate_list);

		//获取线下课程科目，按课程分类
		// $course_list = $this->Courseo2o_model->get_offline_course($this->user_data['school_id']);
		$this->smarty->assign("course_list", array());

		$this->smarty->display("banke/course_banke_publish.html");
	}

	/**
	 * ajax获取课程科目（线下课程edu_o2o_course）
	 *
	 * @param  [type] $type [description]
	 * @return [type]       [description]
	 */
	public function get_o2o_course_list_ajax($cate_id)
	{
		$this->load->model("Courseo2o_model");
		$course_list = $this->Courseo2o_model->get_offline_course($this->user_data['school_id'],$cate_id);

		print_r(json_encode($course_list));exit;
	}

	function shangjia(){
		$post_data = $this->input->post();

		if($post_data['id'] > 0 ){
			// 更新上架状态(下架)
			$sql = "update {$this->db->dbprefix}course set status = 1-status where id ={$post_data['id']}";
			$this->db->query($sql);
			echo 1;exit;
		}


	}

	/**
	*	编辑班课
	*/
	public function edit($id)
	{

		$token = get_qiniu_token('VIMG');
		$this->smarty->assign("token", $token);
		$this->smarty->assign("subject",'');

		$this->load->model('Courseo2o_model');

		$this->smarty->assign("course_id", $id);

		//post data
		$post_data = $this->input->post();

		if($post_data){

			$has_error = 0;
			if(empty($post_data['title'])){
		        $this->smarty->assign("title_err", "班课标题不能为空");
		        $has_error = 1;
	      	}else{
				$this->smarty->assign("title", $post_data['title']);
			}
			if(empty($post_data['course_time'])){
		        $this->smarty->assign("course_time_err", "请填写总课时");
		        $has_error = 1;
	      	}else{
				$this->smarty->assign("course_time", $post_data['course_time']);
			}
			if(!empty($post_data['price'])){
        		$this->smarty->assign("price", $post_data['price']);
			}
			if(!empty($post_data['class_number'])){

				$this->smarty->assign("class_number", $post_data['class_number']);
			}
			if(!empty($post_data['suitable'])){

				$this->smarty->assign("suitable", $post_data['suitable']);
			}
			if(!empty($post_data['learn_what'])){

				$this->smarty->assign("learn_what", $post_data['learn_what']);
			}
			if(empty($post_data['subject'])){
				$this->smarty->assign("subject_err", "请选择科目");
				$this->smarty->assign("subject","");
				$has_error = 1;
			}else{
				$this->smarty->assign("subject", $post_data['subject']);
			}

			if(empty($post_data['detail'])){
				$this->smarty->assign("desc_err", "请填写课程详情");
				$has_error = 1;
			}else{
				$this->smarty->assign("detail", $post_data['detail']);
			}

			if(!empty($post_data['course_imgs'])){
				$imgs = array();
				$i = 0;
				foreach ($post_data['course_imgs'] as $k => $v) {
					if($i > 5){
						break;
					}
					$imgs[] = get_qiniu_url($v);
					$i++;
				}
				$post_data['course_imgs'] = $imgs;
			}
			//插入班课
			if($has_error==0){
				// print_r($post_data);exit;
				$post_data['is_recomend'] = isset($post_data['is_recomend']) ? $post_data['is_recomend'] : 0;
				$this->Courseo2o_model->edit_course_by_school($id,$post_data);
				header("Location:/course_banke");exit;
			}

			// print_r($post_data['img_url'][0]);
			// print_r(json_encode($post_data['img_url']));exit;
		}else{
			/**GET*/
			//获取课程信息
			$course = $this->Courseo2o_model->get_course_by_id($id);
			if(empty($course)){
				header("Location:/course_banke");exit;
			}
			$this->smarty->assign("title", $course['title']);
			$img_url = json_decode($course['course_imgs']);
			$this->smarty->assign("img_url", $img_url);
			$this->smarty->assign("course_time", $course['course_time']);
			$this->smarty->assign("price", $course['price']);
			$this->smarty->assign("class_number", $course['class_number']);
			$this->smarty->assign("suitable", $course['suitable']);
			$this->smarty->assign("learn_what", $course['learn_what']);
			$this->smarty->assign("detail", $course['detail']);
			$this->smarty->assign("cate_id", $course['cate_id']);
			$this->smarty->assign("is_recomend", $course['is_recomend']);
			//取线下课程分类
	    	$cate_list = $this->Courseo2o_model->get_offline_cate($this->user_data['school_id']);
	    	$this->smarty->assign("cate_list", $cate_list);
	    	//获取子课程

			$course_list = $this->Courseo2o_model->get_offline_course($this->user_data['school_id'],$course['cate_id']);
			$this->smarty->assign("course_list", $course_list);



			//获取课程科目
			$subject = $this->Courseo2o_model->get_course_subject_list($id);
			$subject_id_arr = array();
			foreach ($subject as $sub) {
				$subject_id_arr[] = $sub['subject_id'];
			}


			$this->smarty->assign("subject", $subject_id_arr);
		}

		$this->smarty->display("banke/course_banke_edit.html");
	}
}
